package com.qf.blog.service.impl;

import com.qf.blog.commons.constants.SysConsts;
import com.qf.blog.entity.DiscussPost;
import com.qf.blog.entity.User;
import com.qf.blog.service.ICommentService;
import com.qf.blog.service.IPostScoreService;
import com.qf.blog.service.IUserService;
import com.qf.blog.vo.LikeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PostScoreServiceImpl implements IPostScoreService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private PostServiceImpl postService;

    @Autowired
    private ICommentService commentService;

    @Autowired
    private LikeServiceImpl likeService;

    @Autowired
    private IUserService userService;

    @Override
    public void addPostIdToRedis(Integer postId) {
        stringRedisTemplate.opsForSet().add(SysConsts.RedisKeys.POST_SCORE_KEY, postId.toString());
    }

    /**
     * 实现分数的计算
     *
     * @param postId
     */
    @Override
    public Double computeScore(Integer postId) {
        // 1. 根据帖子Id查询帖子信息
        DiscussPost post = postService.getById(postId);
        // 2.获取它的姿态(加精)
        Integer status = post.getStatus();
        LikeVo likeVo = new LikeVo();
        likeVo.setEntityId(postId);
        likeVo.setEntityType(1);
        // 3.获取帖子的点赞数量
        Long likeCount = likeService.getLikeCount(likeVo);
        // 4.查询帖子下面的评论数量
        Long commentCount = commentService.queryCommentCountByPostId(postId);
        log.info("点赞数量"+likeCount);
        log.info("评论数量"+commentCount);
        // 5.计算分数
        Integer userIdLevel = 3; // 作者的权重
        int weight = status == 1 ? 100 : 0; // 如果加精了，多个100
        long _score = (likeCount * 100) + (commentCount * 200) + weight + (userIdLevel * 50);
        Double score = Math.log((double) _score);
        return score;
    }

}
